<?php

/**
 * Created by PhpStorm.
 * User: 王琳
 * Date: 2015/12/15
 * Time: 14:02
 */
/*健康平台报告管理模块*/
class ReportAction extends  Action{

    //市区级
    /*市区级报告  筛选*/
    public function show_report_screen(){
        $city=$_GET["city"];
        $area=$_GET["area"];
        $uname=$_GET["uname"];
		$this->assign("province",model("Area")->where("pid=0")->findALL());
        $data=$this->get_report_data($city,$area,$uname,1,0,0);
        $this->assign("data",$data);
        $this->display("admin_report_screen");
    }
    //园级报告
    public function show_garden_reports(){
        $city=$_GET["city"];
        $area=$_GET["area"];
        $uname=$_GET["uname"];
        $test_type=$_GET["test_type"];
        $year=$_GET["year"];
		$this->assign("province",model("Area")->where("pid=0")->findALL());
        $data=$this->get_report_data($city,$area,$uname,2,$test_type,$year);
        $this->assign("data",$data);
        $this->display("admin_garden_stamp");
    }
    function get_report_data($city,$area,$uname,$type,$test_type,$year){
        $where["is_report"]=1;
        
        if($city>0){
            if($area>0){
                if($type==2){
                    $map["user_group_id"]=4;
                    $where["type"]=3;
                }else{
                    $map["user_group_id"]=3;
                    $where["type"]=2;
                }
                $uids=model("User")->where("area=".$area)->field("uid")->select();
                $uids=getSubByKey($uids,"uid");
                $map["uid"]=array("in",$uids);
                $user=model("UserGroupLink")->where($map)->field("uid")->select();
                $uids=getSubByKey($user,"uid");
                $where["fid"]=array("in",$uids); 
            }else{
                $uids=model("User")->where("city=".$city)->field("uid")->select();
                $uids=getSubByKey($uids,"uid");
                $map["uid"]=array("in",$uids);
                if( $type == 2){
                    $map["user_group_id"]=4;
                } else {
                    $map["user_group_id"]=2;
                }
                $user=model("UserGroupLink")->where($map)->field("uid")->select();
                $uids=getSubByKey($user,"uid");
                $where["fid"]=array("in",$uids); 
                $where["type"]=1;
            }
        } else {
            if( $type==1 ){
                $where["type"] = array('in' , array('1','2'));
            } else if( $type==2 ){
                $where["type"] = 3;
            }
        }

        
        if($uname){
           if($type==2){
                $school=D("school")->where(array("name"=>array("like","%".$uname."%")))->field("fid")->select();
                $uids=getSubByKey($school,"fid");
                $where["fid"]=array("in",$uids);
            }else{
                $area=model("Area")->where("title='".$uname."'")->find();
                if($area){
                    if(model("Area")->where("pid=".$area["area"])->find()){
                        $where["type"]=1;
                        $uids=model("User")->where("city=".$area["area_id"])->field("uid")->select();
                        $uids=getSubByKey($uids,"uid");
                        $map["uid"]=array("in",$uids);
                        $map["user_group_id"]=2;
                        $user=model("UserGroupLink")->where($map)->field("uid")->find();
                        $where["fid"]=$user["uid"];
                    }else{
                        $where["type"]=2;
                        $uids=model("User")->where("area=".$area["area_id"])->field("uid")->select();
                        $uids=getSubByKey($uids,"uid");
                        $map["uid"]=array("in",$uids);
                        $map["user_group_id"]=3;
                        $user=model("UserGroupLink")->where($map)->field("uid")->find();
                        $where["fid"]=array("in",$user["uid"]);
                    }
                }
            }
        }
        if($year>0){
            $where["year"]=$year;
        }
        if($test_type>0){
            $where["test_type"]=$test_type;
        }
        $data=D("HealthReport")->where($where)->order('year desc')->findPage(15);
        foreach ($data['data'] as $key => $value) {
            $data["data"][$key]['name']=$this->_getname($value["type"],$value["fid"]);
            $data["data"][$key]['num']=$this->_gethealthnumber($value["type"],$value["fid"],$value["year"],$value["is_report"],$value["test_type"]);
        }
        return $data;
    }
    //获取该地区下学生人数和录入人数
    function _gethealthnumber($type,$fid,$year,$is_report,$test_number){
        $sum=0;
        if($type==1){
            $user=model("User")->where("uid=".$fid)->field("city")->find();
            $where["city"]=$user["city"];
        }else if($type==2){
            $user=model("User")->where("uid=".$fid)->field("area")->find();
            $where["area"]=$user["area"];
        }else{
            $where["fid"]=$fid;
        }
        $school=D("school")->where($where)->field("id")->select();
        $school_ids=getSubByKey($school,'id');
        unset($where);
        $where["school_id"]=array("in",$school_ids);
        $sum = M("student")->where($where)->count();

		$student = M("student")->where($where)->field("stu_id")->select();
		
        $stu_ids=getSubByKey($student,"stu_id");
        $map["stu_id"]=array("in",$stu_ids);
        $map["year"]=$year;
        $map["is_report"]=$is_report;
        $map["test_number"]=$test_number;
	$map['is_del'] = 0;
        $test_count=M("test_data")->where($map)->count();

        $data["sum"]      = $sum;
        $data["test_num"] = $test_count;
        return $data;
    }
    //获取报告所属市/区/园名
    function _getname($type,$uid){
        $user=model("User")->where("uid=".$uid)->find();
        if($type==3){
            return $user["uname"];
        }else{
            if($type==1){
                $area=model("Area")->where("area_id=".$user["city"])->find();
            }else{
                $area=model("Area")->where("area_id=".$user["area"])->find();
            }
            return $area["title"];
        }
    }
    /*#市区级报告  打印#*/
    public function show_report_stamp(){
        $this->display("admin_report_stamp");
    }

    /*市区级报告统计*/
    public function show_statistics(){
        $area_name = $_REQUEST['area_name'];
        $is_over = $_REQUEST['is_over'];
        $arr = D("Report")->statistics_count($area_name,$is_over);
        $result = D("Report")->get_report_data($area_name,$is_over);
        $this->assign("number",$arr[0]['count_number']);
        $this->assign("result",$result);
        $this->display("admin_report_statistics");
    }

    /*#园级报告   统计#*/
    public function show_report_statistics(){
        $this->display("admin_report_statistics");
    }



//评价报告
    /*#显示评价报告#*/
    public function show_report_evaluate(){
        $city=$_GET["city"];
        $area=$_GET["area"];
        $test_type=$_GET["test_type"];
        $year=$_GET["year"];
        if($area>0){
            $where["type"]=2;
            $uids=model("User")->where("area=".$area)->field("uid")->select();
            $uids=getSubByKey($uids,"uid");
            $map["uid"]=array("in",$uids);
            $user=model("UserGroupLink")->where($map)->field("uid")->select();
            $uids=getSubByKey($user,"uid");
            if($uids){
                $where["fid"]=array("in",$uids); 
            }
        }else{
            if($city>0){
                $where["type"]=1;
                $uids=model("User")->where("city=".$city)->field("uid")->select();
                $uids=getSubByKey($uids,"uid");
                $map["uid"]=array("in",$uids);
                $user=model("UserGroupLink")->where($map)->field("uid")->select();
                $uids=getSubByKey($user,"uid");
                if($uids){
                    $where["fid"]=array("in",$uids); 
                }
            }
        }
        if($test_type>0){
            $where["test_type"]=$test_type;
        }
        if($year>0){
            $where["year"]=$year;
        }
        if($where){
            $data=D("HealthReport")->where($where)->find();
            $this->assign("data",$data);
        }
        $this->assign("province",model("Area")->where("pid=0")->findALL());
        $this->display("admin_report_evaluate");
    }

    public function health_advice(){
        $where['is_del']=0;
        $advice_type= intval($_GET['advice_type']);
        if($advice_type){
            $where['advice_type']=$advice_type;
        }
        $data=M("health_advice")->where($where)->findPage(20);
        foreach ($data["data"] as $key => $value) {
            $data["data"][$key]["attach_image"]=getAttachUrlByAttachId($value["attach_id"]);
        }
        $this->assign("data",$data);
        $this->display();
    }
    public function add_health_advice(){
        $this->display();
    }
    public function addHealthAdvice(){
        $data=array(
            'advice_name'=>$_POST['advice_name'],
            'attach_id'=>intval($_POST['attach_id']),
            'advice_type'=>$_POST['advice_type'],
            'ctime'=>time()
        );
        $res=M("health_advice")->add($data);
        if($res){
            echo $res;
            exit;
        }else{
            echo "添加失败，请重试！";
            exit;
        }
    }
    public function delAdvice(){
        $advice_id=$_POST["id"];
        $data["is_del"]=1;
        if(M("health_advice")->where("advice_id=".$advice_id)->save($data)){
            echo $res;
            exit;
        }else{
            echo "删除失败，请重试！";
            exit;
        }
    }
}
